<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
error_reporting(E_ALL^E_NOTICE^E_WARNING);
class GoodsController extends Yaf_Controller_Abstract {

	public function goodsListAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;
		$model = new GoodsModel();
		$where = [];
		
		$list = $model->getList($where, $page, $pagesize);
		$ids = [];
		foreach($list as $val){
			$ids[] = $val['goods_categoryid'];
		}
		$categoryModel = new GoodscategoryModel();
		$categoryList = $categoryModel->getListByIds($ids);
		
		foreach($list as &$val){
			$val['category'] = $categoryList[$val['goods_categoryid']]['catname'];
		}
		
		$count = $model->getCount($where);
		echo json_encode(['total' => $count, 'list' => $list]);
	}
	
	public function getGoodsAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$id = $request['id'];
		
		$model = new GoodsModel();
		$where = [];
		if($id){
			$where['id = ?'] = $id;
		}
		$rs = $model->getGoods($where);
		echo json_encode($rs);
		
	}
	
	public function saveGoodsAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new GoodsModel();

		$data = [
			'name'             => $request['name'],
			'goods_categoryid' => $request['goods_categoryid'],
			'thumb'            => $request['thumb'],
			'costprice'        => $request['costprice'],
			'currentprice'     => $request['currentprice'],
			'total'            => $request['total'],
			'desc'             => $request['desc'],
			'sellerid'         => $request['sellerid'],
		];
		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->save($data, $where);
		}else{
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
	
	public function getCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$catid = $request['catid'];
		
		$model = new GoodscategoryModel();
		$where = [];
		if($catid){
			$where['catid = ?'] = $catid;
		}
		$rs = $model->getCategory($where);
		echo json_encode($rs);
	}
	
	public function saveCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new GoodscategoryModel();
		//$data = [['catname','listorder','icon'], [$request['catname'], $request['listorder'], $request['icon']]];
		$data['catname'] = $request['catname'];
		$data['listorder'] = $request['listorder'];
		$data['icon'] = $request['icon'];
		if($request['catid']){
			$where['catid = ?'] = $request['catid'];
			
			$rs = $model->save($data, $where);
		}else{
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
	
	public function categoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;

		$model = new GoodscategoryModel();
		$where = [];
		$list = $model->getList($where, $page, $pagesize);
		
		$count = $model->getCount($where);
		echo json_encode(['total' => $count['total'], 'list' => $list]);
	}
	
	public function getAllCategoryAction(){
		$model = new GoodsCategoryModel();
		$where = [];
		$list = $model->getList($where, 0, 0);

		echo json_encode($list);
	}
	
	public function delGoodsAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$model = new GoodsModel();

		if($request['id']){
			$where['id = ?'] = $request['id'];
			
			$rs = $model->delete($where);
			echo json_encode(['message' => '删除成功']);
		}else{
			echo json_encode(['message' => '删除失败']);
		}
		
	}
	
	public function delCategoryAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$model = new GoodscategoryModel();

		if($request['catid']){
			$where['catid = ?'] = $request['catid'];
			
			$rs = $model->delete($where);
			echo json_encode(['message' => '删除成功']);
		}else{
			echo json_encode(['message' => '删除失败']);
		}
		
	}
	
	public function uploadAction(){
		if ((($_FILES["file"]["type"] == "image/gif")
			|| ($_FILES["file"]["type"] == "image/jpeg")
			|| ($_FILES["file"]["type"] == "image/pjpeg"))
			&& ($_FILES["file"]["size"] < 200000)){
			if ($_FILES["file"]["error"] > 0){
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			}else{
				$y = date('Y');
				$m = date('m');
				$d = date('d');
				$midPath = $y.'/'.$m.'/'.$d;
				if (file_exists("../upload/" .$midPath.'/'. $_FILES["file"]["name"])){
				  echo 1;
				}else{
				  if (!file_exists("../upload/" .$midPath)){
					mkdir ("../upload/" .$midPath,0777,true);
				  }
				  move_uploaded_file($_FILES["file"]["tmp_name"],
				  "../upload/" .$midPath.'/'. $_FILES["file"]["name"]);
				  echo "upload/" .$midPath.'/'. $_FILES["file"]["name"];
				}
			}
		  }else{
			echo "Invalid file";
		  }
		
	}
	
}
